﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

namespace MotelServices
{
    public class BBBUS
    {
        public static string _connString = string.Empty;

        #region Static Contructor
        static BBBUS()
        {
            _connString = ConfigurationManager.ConnectionStrings["MotelConnectionString"].ConnectionString.ToString();
        }
        #endregion

        #region Function to get friend
        public static List<BBPDTO> LayBanBe(int maten)
        {
            List<BBPDTO> lbb = new List<BBPDTO>();
            DataMotelDataContext db = new DataMotelDataContext(_connString);
            try
            {
                var BBObj = (from bb in db.BanBes
                                 where bb.TinhTrang.Equals(1) && bb.MaBanBe1.Equals(maten)
                                 select bb);
                foreach (var item in BBObj)
                {
                    BBPDTO bb = new BBPDTO();
                    bb.MaKetBan = Convert.ToInt32(item.MaKetBan);
                    bb.MaBanBe1 = TaiKhoanBUS.LayTenTaiKhoan(item.MaBanBe1);
                    bb.MaBanBe2 = TaiKhoanBUS.LayTenTaiKhoan(Convert.ToInt32(item.MaBanBe2)); 
                    bb.NgayKetBan = Convert.ToDateTime(item.NgayKetBan);
                    bb.TinhTrang = Convert.ToInt32(item.TinhTrang);
                    lbb.Add(bb);
                }
                var BBObj2 = (from bb in db.BanBes
                             where bb.TinhTrang.Equals(1) && bb.MaBanBe2.Equals(maten)
                             select bb);
                foreach (var item in BBObj2)
                {
                        string maban1 = TaiKhoanBUS.LayTenTaiKhoan(item.MaBanBe1);
                        string maban2 = TaiKhoanBUS.LayTenTaiKhoan(Convert.ToInt32(item.MaBanBe2));
                        BBPDTO bb = new BBPDTO();
                        bb.MaKetBan = Convert.ToInt32(item.MaKetBan);
                        bb.MaBanBe1 = maban2;
                        bb.MaBanBe2 = maban1;
                        bb.NgayKetBan = Convert.ToDateTime(item.NgayKetBan);
                        bb.TinhTrang = Convert.ToInt32(item.TinhTrang);
                        foreach (var item2 in BBObj)
                        {
                            if (item2.MaBanBe1 != item.MaBanBe2 && item2.MaBanBe2 != item.MaBanBe1)
                            {
                                lbb.Add(bb);
                            }
                        }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                db.Dispose();
            }
            return lbb;
        }
        #endregion

        #region Function to get request make friend
        public static List<BBPDTO> RequestFriend(int maten)
        {
            List<BBPDTO> lbb = new List<BBPDTO>();
            DataMotelDataContext db = new DataMotelDataContext(_connString);
            try
            {
                var BBObj = (from bb in db.BanBes
                             where bb.TinhTrang.Equals(0) && bb.MaBanBe1.Equals(maten)
                             select bb);
                foreach (var item in BBObj)
                {
                    string maban1 = TaiKhoanBUS.LayTenTaiKhoan(item.MaBanBe1);
                    string maban2 = TaiKhoanBUS.LayTenTaiKhoan(Convert.ToInt32(item.MaBanBe2)); 
                    BBPDTO bb = new BBPDTO();
                    bb.MaKetBan = Convert.ToInt32(item.MaKetBan);
                    bb.MaBanBe1 = maban1;
                    bb.MaBanBe2 = maban2;
                    bb.NgayKetBan = Convert.ToDateTime(item.NgayKetBan);
                    bb.TinhTrang = Convert.ToInt32(item.TinhTrang);
                    lbb.Add(bb);
                }
                var BBObj2 = (from bb in db.BanBes
                             where bb.TinhTrang.Equals(0) && bb.MaBanBe2.Equals(maten)
                             select bb);
                foreach (var item in BBObj2)
                {
                    string maban1 = TaiKhoanBUS.LayTenTaiKhoan(item.MaBanBe1);
                    string maban2 = TaiKhoanBUS.LayTenTaiKhoan(Convert.ToInt32(item.MaBanBe2)); 
                    BBPDTO bb = new BBPDTO();
                    bb.MaKetBan = Convert.ToInt32(item.MaKetBan);
                    bb.MaBanBe1 = maban2;
                    bb.MaBanBe2 = maban1;
                    bb.NgayKetBan = Convert.ToDateTime(item.NgayKetBan);
                    bb.TinhTrang = Convert.ToInt32(item.TinhTrang);
                    foreach (var item2 in BBObj)
                    {
                        if (item2.MaBanBe1 != item.MaBanBe2 && item2.MaBanBe2 != item.MaBanBe1)
                        {
                            lbb.Add(bb);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                db.Dispose();
            }
            return lbb;
        }
        #endregion

        #region Function to send request make friend
        public static bool SendRequestFriend(BBDTO request)
        {
            bool result = true;
            DataMotelDataContext db = new DataMotelDataContext(_connString);
            try
            {
                BBDTO bb = request;
                bb.TinhTrang = 0;
                int rs = db.spInsertSendRequestFriend(bb.MaBanBe1, bb.MaBanBe2, bb.NgayKetBan, bb.TinhTrang);
                if (rs == -1)
                {
                    throw new Exception("Insert not successfully");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                db.Dispose();
            }
            return result;
        }
        #endregion

        #region Function to agree make friend
        public static bool AgreeMakeFriend(BBDTO request)
        {
            bool result = true;
            DataMotelDataContext db = new DataMotelDataContext(_connString);
            try
            {
                BBDTO bb = request;
                bb.TinhTrang = 1;
                int rs = db.spAgreeMakeFriend(bb.MaKetBan,bb.TinhTrang);
                if (rs == -1)
                {
                    throw new Exception("Update not successfully");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                db.Dispose();
            }
            return result;
        }
        #endregion

        #region delete friend or refuse request friend information function
        public static bool deleteFriend(int maketban)
        {
            bool result = true;
            DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
            try
            {
                if (maketban == 0)
                {
                    throw new Exception("Input invalid parameter");
                }
                int ma = maketban;
                int rs = dbContext.spDeleteFriend(ma);
                if (rs == -1)
                {
                    throw new Exception("Delete not successfully");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dbContext.Dispose();
            }
            return result;
        }
        #endregion

        #region search friends
        public static List<TaiKhoanDTO> resultfindfriend(int ma, string text)
        {
            List<TaiKhoanDTO> lbb = new List<TaiKhoanDTO>();
            DataMotelDataContext db = new DataMotelDataContext(_connString);
            try
            {
                var BBObj = (from tk in db.TaiKhoans
                             where tk.TenTaiKhoan.Contains(text) && db.TaiKhoans.All(ban => tk.MaTaiKhoan != ma)
                             select tk);
                foreach (var item in BBObj)
                {
                    TaiKhoanDTO bb = new TaiKhoanDTO();
                    bb.MaTaiKhoan = Convert.ToInt32(item.MaTaiKhoan);
                    bb.TenTaiKhoan = item.TenTaiKhoan;
                    int kt = kiemtrabanbe(ma, Convert.ToInt32(item.MaTaiKhoan));
                    bb.HinhDaiDien = Convert.ToString(kt);
                    lbb.Add(bb);
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                db.Dispose();
            }
            return lbb;
        }
        #endregion

        #region kiem tra  ban be
        public static int kiemtrabanbe(int ma1, int ma2)
        {
            int kt = 0;
            DataMotelDataContext db = new DataMotelDataContext(_connString);
            try
            {
                var BBobj1 = (from bb in db.BanBes
                              where bb.MaBanBe1.Equals(ma1) && bb.MaBanBe2.Equals(ma2)
                              select bb);
                foreach (var item in BBobj1)
                {
                    if (item.TinhTrang == 1)
                        return kt = 1;
                    else
                        return kt = 2;
                }

                var BBobj2 = (from bb in db.BanBes
                              where bb.MaBanBe2.Equals(ma1) && bb.MaBanBe1.Equals(ma2)
                              select bb);
                foreach (var item in BBobj2)
                {
                    if (item.TinhTrang == 1)
                        return kt = 1;
                    else
                        return kt = 2;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                db.Dispose();
            }
            return kt;
        }

        #endregion
    }
}